package com.ymatou.app.rec.service;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;
/**
*视频直播搜索接口Call
*FreeMaker Template自动生成代码
*/
public class C2cCall extends YmatouBaseCall{
	private static String opurl ="search/c2c";
	public C2cCall() {
		super(EnvSetup.getSystemUrl("c2cLiveBizer"),opurl,"GET","PARAM");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	  // 获取status
	public String getStatus() {
		String status = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			status = jo.getString("status");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}

	/**
	 * 获取total
	 * 
	 * @return int
	 */
	public int getTotal() {
		int total = 0;
		try {
			total = Integer.valueOf(new JSONObject(getReturnData())
					.getString("total"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return total;
	}
	
	/*
	 *  获取hit->id值
	 *  @return 
	 * 
	 * */
	/*public JSONObject getHits(){
		JSONObject Data = null;
		try {
			Data = new JSONObject(getReturnData()).getJSONObject("hits");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}
	*/
	/**
	 * 获取接口返回数据的记录
	 * */
	public JSONArray getHits(){
		JSONArray list = null;
		try {
			JSONObject jsonsList = new JSONObject(getReturnData());
			list = jsonsList.getJSONArray("hits");
			if(list.toString().equals("[]")){
				return null;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	/**
	 * 返回stats里的数据
	 * */
	public String getStats(){
		String list = null;
		
		try {
			JSONObject	jsonsList = new JSONObject(getReturnData());
			list = jsonsList.getString("stats");
			/*list = jsonsList.getJSONArray("stats");
			if(list.toString().equals("[]")){
				return null;
			}*/
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 判断接口返回key中是否存在stats
	 * */
	public boolean statsRestul(){
		boolean statsRestul = false;
		try {
			JSONObject	jsonsList = new JSONObject(getReturnData());
			 statsRestul = jsonsList.has("stats");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return statsRestul;
		
	}
}